package com.tuniu.agents.impl.evaluator.impl;

import java.util.List;
import java.util.Map;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.ChatOptions;

import com.tuniu.agents.advisor.AgentChatMemoryAdvisor;
import com.tuniu.agents.agent.AgentManagerGroup;
import com.tuniu.agents.agent.AgentOptions;
import com.tuniu.agents.impl.evaluator.EvaluatorAgent;

import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Flux;

@Slf4j
public class FavoritesEvaluatorAgent extends EvaluatorAgent {
    
    private static final String HISTORY_CONVERSATION = "history_conversation";
    private static final String PRODUCT_DATA = "product_data";
       /**
         * Fixed input
         */
        private static final String QUERY_STRING = "User historical conversation: %s, based on the historical conversation, please help me evaluate the rationality of the plan.";

    public FavoritesEvaluatorAgent(AgentManagerGroup agentManagerGroup,
                            String agentId, String promptTemplateId, ChatOptions chatOptions) {
        super(agentManagerGroup,new AgentOptions(agentId, promptTemplateId, null, null, chatOptions));

    }

    /**
     * Shopping Cart Evaluator Agent Response
     * @param conversationId conversation ID
     * @param message user message
     * @return response
     */
    @Override
    public ChatResponse callResponse(String conversationId, List<Message> messages, Map<String, Object> context) {
        String message = messages.get(0).getText();
        //Generate a random number using current timestamp
        
        if(message.contains("|")){
            String[] messageArray = message.split("\\|");
            String historyString = messageArray[0];
            String itemsString = messageArray[1];
            return chatClient.prompt()
                .system(s -> s.param(HISTORY_CONVERSATION, historyString)
                .param(PRODUCT_DATA, itemsString)) // Set resources to be packaged and selected resources in the prompt
                    .user(QUERY_STRING) //Fixed format input
                    .advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId).
                    param(AgentChatMemoryAdvisor.CHAT_MEMORY_AGENT_ID_KEY, this.agentId))
                    .call()
                    .chatResponse();
    
        }
        return null;
    }

    @Override
    public Flux<String> stream(String conversationId, List<Message> messages, Map<String, Object> context) {
        String message = messages.get(0).getText();
        //Generate a random number using current timestamp
        if(message.contains("|")){
            String[] messageArray = message.split("\\|");
            String historyString = messageArray[0];
            String itemsString = messageArray[1];
            return chatClient.prompt()
                    .system(s -> s.param(PRODUCT_DATA, itemsString)) // Set resources to be packaged and selected resources in the prompt
                    .user(String.format(QUERY_STRING,historyString)) //Fixed format input
                    .advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, conversationId).
                            param(AgentChatMemoryAdvisor.CHAT_MEMORY_AGENT_ID_KEY, this.agentId))
                    .stream()
                    .content();
        }
        return null;
    }


} 